package com.cmnit.dao;

import com.cmnit.bean.DangerousTruckBean;
import com.cmnit.utils.ConfigurationManager;
import com.cmnit.utils.HttpUtils;
import com.cmnit.utils.JSONUtils;
import com.cmnit.utils.OracleUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class DangerousTruckDao {
    private static final Logger log = Logger.getLogger(DangerousTruckDao.class);

    /**
     * 获取总页数
     *
     * @param param 参数
     * @return 总页数
     */
    public Integer getPageInfo(String param) {
        // 获取post参数，发送请求
        String result = HttpUtils.sendPost(ConfigurationManager.getProperty("http.post.url"), param);

        // 获取总页数
        String pages = JSONUtils.getKeyString(result, ConfigurationManager.getProperty("dangerousTruck.json.data"), ConfigurationManager.getProperty("dangerousTruck.json.pages"));
        String current = JSONUtils.getKeyString(result, ConfigurationManager.getProperty("dangerousTruck.json.data"), ConfigurationManager.getProperty("dangerousTruck.json.current"));
        log.info("页数：" + current + "/" + pages);
        return Integer.parseInt(pages);
    }

    /**
     * 获取数据
     *
     * @param param 参数
     * @return 具体数据
     */
    public String getRecords(String param) {
        return JSONUtils.getKeyString(HttpUtils.sendPost(ConfigurationManager.getProperty("http.post.url"), param), ConfigurationManager.getProperty("dangerousTruck.json.data"), ConfigurationManager.getProperty("dangerousTruck.json.records"));
    }

    /**
     * 插入数据
     *
     * @param list 车辆数据
     */
    public void updateRecords(List<DangerousTruckBean> list) {
        // 写入数据
        List<String> records = new ArrayList<>();
        for (DangerousTruckBean dangerousTruck : list) {
            records.add(dangerousTruck.getChepaihm() + "," + dangerousTruck.getChepaiys() + "," + dangerousTruck.getChelianglx());
        }
        OracleUtils.insert(ConfigurationManager.getProperty("dangerousTruck.oracle.tableName"), records, ConfigurationManager.getProperty("dangerousTruck.oracle.tableField"));
    }

    /**
     * 清理原表数据
     */
    public void deleteRecords() {
        OracleUtils.delete(ConfigurationManager.getProperty("dangerousTruck.oracle.tableName"));
    }
}
